package com.landinfo.service.impl;

import com.landinfo.service.SampleLineMonitorService;
import com.landinfo.domain.SampleLineMonitor;
import com.landinfo.repository.SampleLineMonitorRepository;
import com.landinfo.service.dto.SampleLineMonitorDTO;
import com.landinfo.service.mapper.SampleLineMonitorMapper;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing SampleLineMonitor.
 */
@Service
@Transactional
public class SampleLineMonitorServiceImpl implements SampleLineMonitorService{

    private final Logger log = LoggerFactory.getLogger(SampleLineMonitorServiceImpl.class);

    private final SampleLineMonitorRepository sampleLineMonitorRepository;

    private final SampleLineMonitorMapper sampleLineMonitorMapper;

    public SampleLineMonitorServiceImpl(SampleLineMonitorRepository sampleLineMonitorRepository, SampleLineMonitorMapper sampleLineMonitorMapper) {
        this.sampleLineMonitorRepository = sampleLineMonitorRepository;
        this.sampleLineMonitorMapper = sampleLineMonitorMapper;
    }

    /**
     * Save a sampleLineMonitor.
     *
     * @param sampleLineMonitorDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public SampleLineMonitorDTO save(SampleLineMonitorDTO sampleLineMonitorDTO) {
        log.debug("Request to save SampleLineMonitor : {}", sampleLineMonitorDTO);
        List<Double[]> pointList = sampleLineMonitorDTO.getGeometry();
        if(pointList.size() < 2){
            return null;
        }
        Integer srid = 4326;
        GeometryFactory factory = new GeometryFactory();  // getGeometry().getFactory();
        Coordinate[] coordinates = new Coordinate[pointList.size()];
        for (int j = 0; j < pointList.size(); j++) {
            Double[] xy =  pointList.get(j);
            if (xy.length >= 2 && xy[0] != null && xy[1] != null) {
                coordinates[j] = new Coordinate(xy[0], xy[1]);
            }
        }
        LineString lr = factory.createLineString(coordinates);
        lr.setSRID(srid);
        SampleLineMonitor sampleLineMonitor = sampleLineMonitorMapper.toEntity(sampleLineMonitorDTO);
        // toEntity()未转换geometry类型字段，需单独转换一次
        sampleLineMonitor.setGeometry(lr);
        sampleLineMonitor = sampleLineMonitorRepository.save(sampleLineMonitor);
        return sampleLineMonitorMapper.toDto(sampleLineMonitor);
    }

    /**
     *  Get all the sampleLineMonitors.
     *
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<SampleLineMonitorDTO> findAll() {
        log.debug("Request to get all SampleLineMonitors");
        return sampleLineMonitorRepository.findAll().stream()
            .map(sampleLineMonitorMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     *  根据用户名查询所有样线
     *
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<SampleLineMonitorDTO> getAllSampleLineMonitorsByUser(String userName) {
        log.debug("Request to get all SampleLineMonitors");
        String likeUserName = "%|" + userName + "|%";
        return sampleLineMonitorRepository.queryAllByUser(likeUserName).stream()
            .map(sampleLineMonitorMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     *  Get one sampleLineMonitor by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public SampleLineMonitorDTO findOne(Long id) {
        log.debug("Request to get SampleLineMonitor : {}", id);
        SampleLineMonitor sampleLineMonitor = sampleLineMonitorRepository.findOneById(id);
        SampleLineMonitorDTO sampleLineMonitorDTO = sampleLineMonitorMapper.toDto(sampleLineMonitor);
        return sampleLineMonitorDTO;
    }

    /**
     *  Delete the  sampleLineMonitor by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete SampleLineMonitor : {}", id);
        sampleLineMonitorRepository.deleteById(id);
    }
}
